import Vue from 'vue';
import ajax from 'uni-ajax';
import {  encrypt } from '@/utils/interface';
import crypto from 'crypto-js';
import util from '@/utils';
// const baseURL ='http://192.168.0.112:8081/app';//本地
// const baseURL ='http://47.242.213.23:8082/app';//测试
const baseURL ='http://www.zglh.site:8081/app';//开发
const _ajax =ajax.create(()=>{
	return {
		baseURL,
		method: 'POST',
		header:{
			'x-app-token':uni.getStorageSync('Login').token
		}
	}
});
_ajax.interceptors.request.use(
	async config => {
		if (['object', 'undefined'].includes(typeof config.data)) {
			config.data = {
				...config.data,
			};
		}
		return config;
	}
);
var data=0;
_ajax.interceptors.response.use(
	response => {
		if(response.data.code === 3){//token过期或者账号不存在
			if(data>0) return;
			data=1;
			uni.showModal({
			  content: '登录过期,请重新登录。',
			  showCancel: false,
			  confirmText: '重新登录',
			  success: res => {
				  data=0
				  uni.removeStorageSync('Login');
				  uni.reLaunch({
				  	url:'/pages/login/login'
				  })
			  }
			})
		}else if (response.data.code != 1) {
			util.toast(response.data.msg);
		} 
		return response;
	},
	error => {
		uni.hideLoading();
		return error;
	}
);

Vue.prototype.request = _ajax;
Vue.prototype.encrypt = encrypt;

export default _ajax;
